package me.pearjelly.util;

import java.io.IOException;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class ImageMagickUtil {
	public static void convertSmart(String srcPath, String toPath, int width,
			int height, int maxWidth, int maxHeight) {
		int newWidth = computeNewSize(width, height, maxWidth, maxHeight)[0];
		String command = new StringBuilder("/usr/local/bin/convert -geometry ")
				.append(newWidth).append(" ").append(srcPath).append(" ")
				.append(toPath).toString();
		try {
			Runtime.getRuntime().exec(command);
			logger.debug("execute command:{}", command);
		} catch (IOException e) {
			logger.error("execute command:{}, fails:{}", command,
					e.getMessage());
		}
	}

	public static void convert(String srcPath, String toPath, int width,
			int height, int maxWidth, int maxHeight) {
		convertSmart(srcPath, toPath, width, height, maxWidth, maxHeight);
	}

	public static void convertFixedHeight(String srcPath, String toPath,
			int width, int height, int maxWidth, int maxHeight) {
		String command;
		if (width > height) {
			command = new StringBuilder("/usr/local/bin/convert -geometry ")
					.append(maxWidth).append("x").append(maxHeight)
					.append("! ").append(srcPath).append(" ").append(toPath)
					.toString();
		} else {
			command = new StringBuilder("/usr/local/bin/convert -geometry x")
					.append(maxHeight).append(" ").append(srcPath).append(" ")
					.append(toPath).toString();
		}
		try {
			Runtime.getRuntime().exec(command);
			logger.debug("execute command:{}", command);
		} catch (IOException e) {
			logger.error("execute command:{}, fails:{}", command,
					e.getMessage());
		}
	}

	public static void convertFixedWidth(String srcPath, String toPath,
			int width, int height, int maxWidth, int maxHeight) {
		String command;
		if (width < height) {
			command = new StringBuilder("/usr/local/bin/convert -geometry ")
					.append(maxWidth).append("x").append(maxHeight)
					.append("! ").append(srcPath).append(" ").append(toPath)
					.toString();
		} else {
			command = new StringBuilder("/usr/local/bin/convert -geometry ")
					.append(maxWidth).append("x ").append(srcPath).append(" ")
					.append(toPath).toString();
		}
		try {
			Runtime.getRuntime().exec(command);
			logger.debug("execute command:{}", command);
		} catch (IOException e) {
			logger.error("execute command:{}, fails:{}", command,
					e.getMessage());
		}
	}

	private static int[] computeNewSize(float width, float height,
			float maxWidth, float maxHeight) {
		float newWidth = width;
		float newHeight = height;
		float newRate = 1;
		while (newHeight > maxHeight || newWidth > maxWidth) {
			if (newRate < (newWidth / maxWidth)) {
				newRate = (newWidth / maxWidth);
			} else {
				newRate = newHeight / maxHeight;
			}
			newHeight = newHeight / newRate;
			newWidth = newWidth / newRate;
			if (newWidth > maxWidth) {
				newRate = (newWidth / maxWidth);
				newWidth = newWidth / newRate;
				newHeight = newHeight / newRate;
			}
			if (newHeight > maxHeight) {
				newRate = (newHeight / maxHeight);
				newWidth = newWidth / newRate;
				newHeight = newHeight / newRate;
			}
		}
		float style = newWidth / newHeight;
		int styleInt = 0;
		if (style >= 0.875 && style <= 1.14) {
			styleInt = 0;
		} else if (style > 1) {
			styleInt = 1;
		} else {
			styleInt = 2;
		}
		int[] result = { (new Float(newWidth)).intValue(),
				(new Float(newHeight)).intValue(), styleInt };
		return result;
	}

	private static final Logger logger = LoggerFactory
			.getLogger(ImageMagickUtil.class);
}
